package com.matrix.draft.sockettest.day5;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

public class ZeroCopyClient {
    public static void main(String[] args) {
        try {
            SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 8080));
            socketChannel.configureBlocking(true);
            FileChannel fileChannel = new FileInputStream("D:\\res\\temp\\zero.rar").getChannel();
            // transferTo底层使用0拷贝
            long start = System.currentTimeMillis();
            long totalSendCount = fileChannel.transferTo(0, fileChannel.size(), socketChannel);
            System.err.println("共发送数据:"+totalSendCount+"字节");
            System.err.println("耗时:"+(System.currentTimeMillis()-start));
            fileChannel.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
